package com.lanou3g.mynewstest.module.news.headline;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.beans.NewsHeadlineBeans;
import com.lanou3g.mynewstest.beans.NewsPhotoSetBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.tools.StringRequestUtf;

import java.util.ArrayList;

/**
 * Created by dllo on 16/1/8.
 */
public class HeadlineADSAdapter extends PagerAdapter {

    private ArrayList<View> views;
    private Context context;
    private NewsHeadlineBeans data;
    private NewsPhotoSetBeans beans;

    public HeadlineADSAdapter(Context context, NewsHeadlineBeans data) {
        this.context = context;
        this.data = data;
        initData();
    }

    private void initData() {
        views = new ArrayList<>();

        for (int i = 0; i < 4; i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_headline_ads, null);
            NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.item_headline_ads_iv);
            MyRequestQueue.setNetImage(imageView, data.getT1348647909107().get(0).getAds().get(i).getImgsrc());
            TextView title = (TextView) view.findViewById(R.id.bottom_title_tv);
            title.setText(data.getT1348647909107().get(0).getAds().get(i).getTitle());
            views.add(view);
        }
    }

    @Override
    public int getCount() {

        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final int pos[] = {position % 4};
        View view = views.get(pos[0]);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBeans(pos[0]);
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    private void getBeans(int pos) {
        String url = data.getT1348647909107().get(0).getAds().get(pos).getUrl();
        url = url.replace("|", "/");
        url = url.replace(url.substring(0, 4), "");

        StringRequestUtf request = new StringRequestUtf("http://c.m.163.com/photo/api/set/" + url + ".json",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        beans = gson.fromJson(response, NewsPhotoSetBeans.class);

                        PhotoSetActivity.goToPhotoSetActivity(context, beans);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        MyRequestQueue.addRequest(request);
    }

}
